home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 1680 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.8 KB

  1. Path: news.th-darmstadt.de!news
  2. From: Enno Sandner <enno@intellektik.informatik.th-darmstadt.de>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: IsA v/s HasA problem
  5. Date: Fri, 12 Jan 1996 09:18:47 +0100
  6. Organization: Fachbereich Informatik, TH Darmstadt
  7. Message-ID: <30F61967.167EB0E7@intellektik.informatik.th-darmstadt.de>
  8. References: <30F52C66.851@bangate.compaq.com>
  9. NNTP-Posting-Host: kitz.intellektik.informatik.th-darmstadt.de
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0b4 (X11; I; SunOS 4.1.3 sun4m)
  14.  
  15. Saurabh Dixit wrote:
  16. > Hi!
  17. > I always had no trouble distinguishing between the two...
  18. > But now, I for a specific case, I fail to see the repercussions of
  19. > using the wrong relationship between objects.
  20. > This is my scenario.
  21. > I have an object of type Base.  This is an abstract class.
  22. > From Base I derive several different objects.
  23. > I want to maintain a list of such objects for which I have a class
  24. > Collection that encapsulates a typed array of Base objects.
  25. > I now want to define an object - let's call it Duh - that maintains a
  26. > collection of Base objects.  Duh objects are entities on their own and could
  27. > have other attributes, BUT NEVER ANOTHER COLLECTION.
  28. > From what I gather, I am then leaning towards a HasA relationship, i.e.
  29. > class Collection
  30. > {
  31. > public:
  32. >         BOOL Add (Base* b);
  33. > };
  34. > class Duh
  35. > {
  36. > private:
  37. >         Collection c;
  38. > };
  39. > Obviously if I was going to have 2 or more collections, then I would have had
  40. > to have a HasA relationship.  But as said earlier, this is not the case!
  41. > Also, I want to have some operations common between Collection and Duh.
  42. > e.g. I can Add() a Base (derived!) object to a Collection and I also want
  43. > to Add() an object to Duh.  In a HasA relationship I would simply
  44. > implemenent this as follows.
  45. > BOOL Duh::Add (Base* b)
  46. > {
  47. >         // simply pass on to Collection method
  48. >         return c.Add (b);
  49. > }
  50. > However for an IsA relationship, I would simply do
  51. > class Duh : public Collection
  52. > {
  53. > };
  54. > and I am done.
  55. > Am I wasting my time too much thinking about this?  Regardless of code
  56. > implementation, from the logical perspective, what is the correct object-
  57. > oriented solution for my problem, HasA or IsA?
  58.  
  59. The first thing you should think about, if you are planning to let 'Duh'
  60. and 'Collection' form an ISA relationsship:
  61.  
  62.    is it reasonable to regard 'Duh' as specialization of a 'Collection'
  63.    or do you just 'borrow' the functionality.
  64.  
  65. For example you should not
  66. o narrow the interface
  67. o redefine functions in way that violates the assumptions made in the
  68.   Collection class.
  69.     
  70. If 'Duh' is planned to be a collection with some additional behavior it's
  71. reasonable to derive it public from 'Collection'.
  72. Otherwise the HasA relationsship seems to be the better choice.
  73.  
  74.     Enno
  75.